﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
//using UnityEngine.Monetization;
using UnityEngine.Advertisements;


public class AdsPanel : MonoBehaviour, IUnityAdsListener
{
    public static AdsPanel Instance;

    public Button Button_ads_1;
    public Button Button_ads_2;
    public Button Button_ads_3;
    public Button Button_ads_4;
    public Button Button_ads_5;



    string gameId = "3747945";
    bool testMode = true;
    string myPlacementId = "rewardedVideo";

    private void Awake()
    {
        Instance = this;
    }

    void Start()
    {
        Advertisement.AddListener(this);
        Advertisement.Initialize(gameId, testMode);


        EventTriggerListener.Get(Button_ads_1.gameObject, null).onClick = (GameObject go, object data) =>
        {
            Debug.Log(111);
            //TipPanel.Instance.Open("广告");
            ShowRewardedVideo();
        };

        EventTriggerListener.Get(Button_ads_2.gameObject, null).onClick = (GameObject go, object data) =>
        {
            Debug.Log(222);
            if (Advertisement.IsReady())
            {
                Advertisement.Show();
            }
            else
            {
                Debug.Log("Interstitial ad not ready at the moment! Please try again later!");
            }
        };

        EventTriggerListener.Get(Button_ads_3.gameObject, null).onClick = (GameObject go, object data) =>
        {
            Debug.Log(333);
            StartCoroutine(ShowBannerWhenInitialized());
        };

        EventTriggerListener.Get(Button_ads_4.gameObject, null).onClick = (GameObject go, object data) =>
        {
            Debug.Log(444);

        };

        EventTriggerListener.Get(Button_ads_5.gameObject, null).onClick = (GameObject go, object data) =>
        {
            Debug.Log(555);

        };
    }

    public void ShowRewardedVideo()
    {
        // Check if UnityAds ready before calling Show method:
        if (Advertisement.IsReady(myPlacementId))
        {
            Advertisement.Show(myPlacementId);
        }
        else
        {
            Debug.Log("Rewarded video is not ready at the moment! Please try again later!");
        }
    }

    // Implement IUnityAdsListener interface methods:
    public void OnUnityAdsDidFinish(string placementId, ShowResult showResult)
    {
        // Define conditional logic for each ad completion status:
        if (showResult == ShowResult.Finished)
        {
            Debug.Log("结束");
            // Reward the user for watching the ad to completion.
        }
        else if (showResult == ShowResult.Skipped)
        {
            Debug.Log("跳过");
            // Do not reward the user for skipping the ad.
        }
        else if (showResult == ShowResult.Failed)
        {
            Debug.Log("失败");
            Debug.LogWarning("The ad did not finish due to an error.");
        }
    }

    public void OnUnityAdsReady(string placementId)
    {
        // If the ready Placement is rewarded, show the ad:
        if (placementId == myPlacementId)
        {
            // Optional actions to take when the placement becomes ready(For example, enable the rewarded ads button)
        }
    }

    public void OnUnityAdsDidError(string message)
    {
        // Log the error.
        Debug.Log("失败");
    }

    public void OnUnityAdsDidStart(string placementId)
    {
        // Optional actions to take when the end-users triggers an ad.
        Debug.Log("开始");
    }

    // When the object that subscribes to ad events is destroyed, remove the listener:
    public void OnDestroy()
    {
        Debug.Log("销毁监听");
        Advertisement.RemoveListener(this);
    }



    IEnumerator ShowBannerWhenInitialized()
    {
        while (!Advertisement.isInitialized)
        {
            yield return new WaitForSeconds(0.5f);
        }
        Advertisement.Banner.Show("bannerPlacement");
    }
}
